package com.situ.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Json封装类
 */
public class ResponseData {
//老师代码部分，用于controller
//页面加载有时无缘无故出问题，但又没什么问题，有了这个就跳到友好页面
//private int code; //错误编码
//	private String mess;//错误信息
//	private Map data=new HashMap(); //你啊哟专递的值
//
//	private static ResponseData my;
//
//	public static ResponseData ok(){
//		my=new ResponseData();
//		my.code=200;
//		my.mess="success"+my.code;
//		return my;
//	}
//	//很多 的异常类型 自己去加....
//	public static ResponseData error500(){
//		my=new ResponseData();
//		my.code=500;
//		my.mess="内部错误"+my.code;
//		return my;
//	}
//
//	public static ResponseData put(String key,Object value){
//		my.data.put(key, value);
//		return my;
//	}
//
//	public int getCode() {
//		return code;
//	}
//
//	public void setCode(int code) {
//		this.code = code;
//	}
//
//	public String getMess() {
//		return mess;
//	}
//
//	public void setMess(String mess) {
//		this.mess = mess;
//	}
//
//	public Map getData() {
//		return data;
//	}
//
//	public void setData(Map data) {
//		this.data = data;
//	}

	//自动生成部分
	public static final String ERRORS_KEY = "errors";

	private final String message;
	private final int code;

	private final Map<String, Object> data = new HashMap<>();

	public String getMessage() {
		return message;
	}

	public int getCode() {
		return code;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public ResponseData putDataValue(String key, Object value) {
		data.put(key, value);
		return this;
	}

	private ResponseData(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public static ResponseData ok() {
		return new ResponseData(200, "OK");
	}

	public static ResponseData notFound() {
		return new ResponseData(404, "Not Found");
	}

	public static ResponseData badRequest() {
		return new ResponseData(400, "Bad Request");
	}

	public static ResponseData forbidden() {
		return new ResponseData(403, "Forbidden");
	}

	public static ResponseData unauthorized() {
		return new ResponseData(401, "unauthorized");
	}

	public static ResponseData serverInternalError() {
		return new ResponseData(500, "Server Internal Error");
	}

	public static ResponseData customerError(String message) {
		return new ResponseData(1001, message);
	}
}
